{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "from __future__ import absolute_import\n",
    "from __future__ import division\n",
    "from __future__ import print_function\n",
    "\n",
    "import argparse\n",
    "import sys\n",
    "\n",
    "from tensorflow.examples.tutorials.mnist import input_data\n",
    "\n",
    "import tensorflow as tf\n",
    "\n",
    "FLAGS = None"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "WARNING:tensorflow:From <ipython-input-2-ca270559f1e5>:2: read_data_sets (from tensorflow.contrib.learn.python.learn.datasets.mnist) is deprecated and will be removed in a future version.\n",
      "Instructions for updating:\n",
      "Please use alternatives such as official/mnist/dataset.py from tensorflow/models.\n",
      "WARNING:tensorflow:From /usr/local/lib/python3.6/dist-packages/tensorflow/contrib/learn/python/learn/datasets/mnist.py:260: maybe_download (from tensorflow.contrib.learn.python.learn.datasets.base) is deprecated and will be removed in a future version.\n",
      "Instructions for updating:\n",
      "Please write your own downloading logic.\n",
      "WARNING:tensorflow:From /usr/local/lib/python3.6/dist-packages/tensorflow/contrib/learn/python/learn/datasets/mnist.py:262: extract_images (from tensorflow.contrib.learn.python.learn.datasets.mnist) is deprecated and will be removed in a future version.\n",
      "Instructions for updating:\n",
      "Please use tf.data to implement this functionality.\n",
      "Extracting /data/train-images-idx3-ubyte.gz\n",
      "WARNING:tensorflow:From /usr/local/lib/python3.6/dist-packages/tensorflow/contrib/learn/python/learn/datasets/mnist.py:267: extract_labels (from tensorflow.contrib.learn.python.learn.datasets.mnist) is deprecated and will be removed in a future version.\n",
      "Instructions for updating:\n",
      "Please use tf.data to implement this functionality.\n",
      "Extracting /data/train-labels-idx1-ubyte.gz\n",
      "WARNING:tensorflow:From /usr/local/lib/python3.6/dist-packages/tensorflow/contrib/learn/python/learn/datasets/mnist.py:110: dense_to_one_hot (from tensorflow.contrib.learn.python.learn.datasets.mnist) is deprecated and will be removed in a future version.\n",
      "Instructions for updating:\n",
      "Please use tf.one_hot on tensors.\n",
      "Extracting /data/t10k-images-idx3-ubyte.gz\n",
      "Extracting /data/t10k-labels-idx1-ubyte.gz\n",
      "WARNING:tensorflow:From /usr/local/lib/python3.6/dist-packages/tensorflow/contrib/learn/python/learn/datasets/mnist.py:290: DataSet.__init__ (from tensorflow.contrib.learn.python.learn.datasets.mnist) is deprecated and will be removed in a future version.\n",
      "Instructions for updating:\n",
      "Please use alternatives such as official/mnist/dataset.py from tensorflow/models.\n"
     ]
    }
   ],
   "source": [
    "data_dir = '/data'\n",
    "mnist = input_data.read_data_sets(data_dir, one_hot=True)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "550\n"
     ]
    }
   ],
   "source": [
    "batch_size = 100\n",
    "steps = mnist.train.num_examples // batch_size\n",
    "\n",
    "print(steps)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "x = tf.placeholder(tf.float32,[None,784])\n",
    "y_ = tf.placeholder(tf.float32,[None,10])\n",
    "\n",
    "#keep_prob = tf.placeholder(tf.float32)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [],
   "source": [
    "w1 = tf.Variable(tf.truncated_normal([784,300],stddev = 0.1))  #用最简单的0.1开始\n",
    "b1 = tf.Variable(tf.zeros([300])+0.1)\n",
    "y1 = tf.nn.tanh(tf.matmul(x,w1)+b1)\n",
    "\n",
    "w2 = tf.Variable(tf.truncated_normal([300,500],stddev = 0.1))\n",
    "b2 = tf.Variable(tf.zeros([500])+0.1)\n",
    "y2 = tf.nn.tanh(tf.matmul(y1,w2)+b2)\n",
    "\n",
    "w3 = tf.Variable(tf.truncated_normal([500,300],stddev = 0.1))\n",
    "b3 = tf.Variable(tf.zeros([300])+0.1)\n",
    "y3 = tf.nn.tanh(tf.matmul(y2,w3)+b3)\n",
    "\n",
    "\n",
    "w4 = tf.Variable(tf.truncated_normal([300,10],stddev = 0.1))\n",
    "b4 = tf.Variable(tf.zeros([10])+0.1)\n",
    "y = tf.matmul(y3,w4)+b4\n",
    "y_pre = tf.nn.softmax(y)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "WARNING:tensorflow:From <ipython-input-6-9ae9e20a5d6c>:1: softmax_cross_entropy_with_logits (from tensorflow.python.ops.nn_ops) is deprecated and will be removed in a future version.\n",
      "Instructions for updating:\n",
      "\n",
      "Future major versions of TensorFlow will allow gradients to flow\n",
      "into the labels input on backprop by default.\n",
      "\n",
      "See `tf.nn.softmax_cross_entropy_with_logits_v2`.\n",
      "\n"
     ]
    }
   ],
   "source": [
    "cross_entropy = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(labels=y_, logits=y_pre))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [],
   "source": [
    "train_step = tf.train.GradientDescentOptimizer(0.5).minimize(cross_entropy)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [],
   "source": [
    "sess = tf.Session()\n",
    "init_op = tf.global_variables_initializer()\n",
    "sess.run(init_op)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [],
   "source": [
    "correct_prediction = tf.equal(tf.argmax(y_pre,1),tf.argmax(y_,1))\n",
    "accuracy = tf.reduce_mean(tf.cast(correct_prediction,tf.float32))\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "次数0结果:\n",
      "0.9446\n",
      "次数1结果:\n",
      "0.953\n",
      "次数2结果:\n",
      "0.9644\n",
      "次数3结果:\n",
      "0.9654\n",
      "次数4结果:\n",
      "0.9696\n",
      "次数5结果:\n",
      "0.9727\n",
      "次数6结果:\n",
      "0.9728\n",
      "次数7结果:\n",
      "0.9748\n",
      "次数8结果:\n",
      "0.9758\n",
      "次数9结果:\n",
      "0.9756\n",
      "次数10结果:\n",
      "0.9762\n",
      "次数11结果:\n",
      "0.9772\n",
      "次数12结果:\n",
      "0.9768\n",
      "次数13结果:\n",
      "0.9777\n",
      "次数14结果:\n",
      "0.9781\n",
      "次数15结果:\n",
      "0.9767\n",
      "次数16结果:\n",
      "0.9788\n",
      "次数17结果:\n",
      "0.9793\n",
      "次数18结果:\n",
      "0.9792\n",
      "次数19结果:\n",
      "0.979\n",
      "次数20结果:\n",
      "0.9795\n",
      "次数21结果:\n",
      "0.9794\n",
      "次数22结果:\n",
      "0.9791\n",
      "次数23结果:\n",
      "0.98\n",
      "次数24结果:\n",
      "0.9803\n",
      "次数25结果:\n",
      "0.9796\n",
      "次数26结果:\n",
      "0.9806\n",
      "次数27结果:\n",
      "0.9804\n",
      "次数28结果:\n",
      "0.9805\n",
      "次数29结果:\n",
      "0.9802\n",
      "次数30结果:\n",
      "0.98\n"
     ]
    }
   ],
   "source": [
    "#据说30次，100次，如果30次能收敛，就用30次\n",
    "for ep in range(31):\n",
    "    for batch in range(steps):\n",
    "        batch_xs,batch_ys = mnist.train.next_batch(batch_size)\n",
    "        sess.run(train_step,feed_dict={x:batch_xs,y_:batch_ys})\n",
    "    \n",
    "    print(\"次数\" + str(ep) + \"结果:\")\n",
    "    print(sess.run(accuracy, feed_dict={x: mnist.test.images,y_: mnist.test.labels}))\n",
    "    "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.7rc1"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
